package internal

import (
	"errors"
	"fmt"

	"github.com/EDDYCJY/go-gin-example/models"
	"github.com/jinzhu/gorm"
)

func CalculatePrice(db *gorm.DB, instanceName string, leaseTimeSecond int) (float64, error) {
	if leaseTimeSecond <= 0 {

		return 0, errors.New("租赁时间不能小于等于0秒")
	}
	var price models.Price
	err := db.Model(&models.Price{}).Where(&models.Price{Name: instanceName}).First(&price).Error
	if err != nil {
		return 0, fmt.Errorf("查询价格失败: %v", err.Error())
	}
	amount := CalculateAmount(leaseTimeSecond, price.Price)
	return amount, nil
}
//后面按照 每周 每月 继续计算 还是调用这个函数即可
//前端传递不同的类型 对这个函数进行传递即可
func CalculateAmount(leaseTimeSecond int, price float64) float64 {
	leaseTimehours := leaseTimeSecond / 3600
	//至少租用一个小时
	if leaseTimehours == 0 {
		leaseTimehours = 1
	}
	amount := price * float64(leaseTimehours)
	return amount
}
